#import "OsderSelectSubView.h"
#import <Masonry/Masonry.h>  // 引入Masonry库

@interface OsderSelectSubView ()

@property (nonatomic, strong) NSArray *conditionOptions;
@property (nonatomic, strong) NSArray *colorOptions;
@property (nonatomic, strong) NSArray *rentalPeriodOptions;

@property (nonatomic, strong) NSString *selectedCondition;
@property (nonatomic, strong) NSString *selectedColor;
@property (nonatomic, strong) NSString *selectedRentalPeriod;

@property (nonatomic, strong) UIStackView *mainStackView;

@end

@implementation OsderSelectSubView

- (instancetype)initWithFrame:(CGRect)frame
             conditionOptions:(NSArray<NSString *> *)conditionOptions
                colorOptions:(NSArray<NSString *> *)colorOptions
           rentalPeriodOptions:(NSArray<NSString *> *)rentalPeriodOptions
                   optionSelected:(void (^)(NSString *selectedCondition, NSString *selectedColor, NSString *selectedRentalPeriod))optionSelected {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.conditionOptions = conditionOptions;
        self.colorOptions = colorOptions;
        self.rentalPeriodOptions = rentalPeriodOptions;
        
        self.selectedCondition = self.conditionOptions[0];
        self.selectedColor = self.colorOptions[0];
        self.selectedRentalPeriod = self.rentalPeriodOptions[0];
        
        self.optionSelected = optionSelected;
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    [self setUserInteractionEnabled:true];
    
    self.mainStackView = [[UIStackView alloc] initWithFrame:self.bounds];
    self.mainStackView.axis = UILayoutConstraintAxisVertical;
    self.mainStackView.spacing = 20;
    self.mainStackView.alignment = UIStackViewAlignmentFill;
    self.mainStackView.distribution = UIStackViewDistributionFillEqually;
    self.mainStackView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.mainStackView setUserInteractionEnabled:true];
    [self addSubview:self.mainStackView];
    
    [self.mainStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(20);
        make.right.equalTo(self.mas_right).offset(-20);
    }];
    
    [self.mainStackView addArrangedSubview:[self createOptionViewWithTitle:@"选择成色" options:self.conditionOptions selectedOption:self.selectedCondition section:0]];
    [self.mainStackView addArrangedSubview:[self createOptionViewWithTitle:@"颜色分类" options:self.colorOptions selectedOption:self.selectedColor section:1]];
    [self.mainStackView addArrangedSubview:[self createOptionViewWithTitle:@"租借周期" options:self.rentalPeriodOptions selectedOption:self.selectedRentalPeriod section:2]];
}

- (UIView *)createOptionViewWithTitle:(NSString *)title options:(NSArray *)options selectedOption:(NSString *)selectedOption section:(NSInteger)section {
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.textColor = myRGBHex(0x333333);
    titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    
    UIView *optionView = [[UIView alloc] init];
    optionView.tag = section;
    optionView.translatesAutoresizingMaskIntoConstraints = NO;
    [optionView setUserInteractionEnabled:true];
    [optionView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(optionView.mas_top);
        make.left.equalTo(optionView.mas_left);
        make.right.equalTo(optionView.mas_right);
    }];
    
    UIView *buttonContainer = [[UIView alloc] init];
    [buttonContainer setUserInteractionEnabled:true];
    [optionView addSubview:buttonContainer];
    
    CGFloat topSpacing = 0;
    CGFloat buttonHeight1 = 44;
    [buttonContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(topSpacing);
        make.left.equalTo(optionView.mas_left);
        make.right.equalTo(optionView.mas_right);
        make.height.equalTo(@(buttonHeight1));
        make.bottom.equalTo(optionView.mas_bottom);
    }];
    
    CGFloat buttonWidth = 65.0;
    CGFloat buttonHeight = 24.0;
    CGFloat spacing = 10.0;
    CGFloat xOffset = 0.0;
    
    for (int i = 0; i < options.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:options[i] forState:UIControlStateNormal];
        button.tag = i;
        
        [button.widthAnchor constraintEqualToConstant:buttonWidth].active = YES;
        [button.heightAnchor constraintEqualToConstant:buttonHeight].active = YES;
        
        button.layer.cornerRadius = buttonHeight / 2;
        button.layer.masksToBounds = YES;
        
        button.backgroundColor = [selectedOption isEqualToString:options[i]] ? myRGBHexAlpha(0xFF0AA1, 0.7) : myRGBHexAlpha(0xCCCCCC, 0.2);
        
        [button setTitleColor:[selectedOption isEqualToString:options[i]] ? myRGBHex(0xFFFFFF) : myRGBHex(0x666666) forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(optionSelected:) forControlEvents:UIControlEventTouchUpInside];
        
        [buttonContainer addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(buttonContainer.mas_left).offset(xOffset);
            make.centerY.equalTo(buttonContainer.mas_centerY);
        }];
        
        xOffset += buttonWidth + spacing;
    }
    
    return optionView;
}


- (void)optionSelected:(UIButton *)sender {
    UIButton *button = (UIButton *)sender;
    NSInteger section = button.superview.superview.tag;
    
    if (section == 0) {
        self.selectedCondition = self.conditionOptions[button.tag];
    } else if (section == 1) {
        self.selectedColor = self.colorOptions[button.tag];
    } else if (section == 2) {
        self.selectedRentalPeriod = self.rentalPeriodOptions[button.tag];
    }
    
    [self updateButtonBackgroundColor];
    
    if (self.optionSelected) {
        self.optionSelected(self.selectedCondition, self.selectedColor, self.selectedRentalPeriod);
    }
}

- (void)updateButtonBackgroundColor {
    for (UIView *optionView in self.mainStackView.arrangedSubviews) {
        UIView *buttonContainer = optionView.subviews[1];
        
        for (UIButton *button in buttonContainer.subviews) {
            
            if ([self.conditionOptions containsObject:button.titleLabel.text]) {
                if ([self.selectedCondition isEqualToString:button.titleLabel.text]) {
                    button.backgroundColor = myRGBHexAlpha(0xFF0AA1, 0.7);
                    [button setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
                } else {
                    button.backgroundColor = myRGBHexAlpha(0xCCCCCC, 0.2);
                    [button setTitleColor:myRGBHex(0x666666) forState:UIControlStateNormal];
                }
            }
            
            if ([self.colorOptions containsObject:button.titleLabel.text]) {
                if ([self.selectedColor isEqualToString:button.titleLabel.text]) {
                    button.backgroundColor = myRGBHexAlpha(0xFF0AA1, 0.7);
                    [button setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
                } else {
                    button.backgroundColor = myRGBHexAlpha(0xCCCCCC, 0.2);
                    [button setTitleColor:myRGBHex(0x666666) forState:UIControlStateNormal];
                }
            }
            
            if ([self.rentalPeriodOptions containsObject:button.titleLabel.text]) {
                if ([self.selectedRentalPeriod isEqualToString:button.titleLabel.text]) {
                    button.backgroundColor = myRGBHexAlpha(0xFF0AA1, 0.7);
                    [button setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
                } else {
                    button.backgroundColor = myRGBHexAlpha(0xCCCCCC, 0.2);
                    [button setTitleColor:myRGBHex(0x666666) forState:UIControlStateNormal];
                }
            }
        }
    }
}



@end
